(function($) {
	/***************************************************************************
	 * JQuery 表单输入插件12.0
	 * 
	 * Author: 姜志光 Date: 2014年1月8日
	 **************************************************************************/

	FormInput = function(rootElement, userSettings) {

		var instance = this;

		/***********************************************************************
		 * 表单输入设计配置
		 **********************************************************************/
		var designConfig = userSettings.designConfig;

		/***********************************************************************
		 * 表单输入输入配置
		 **********************************************************************/
		var inputConfig = userSettings.inputConfig;

		/***********************************************************************
		 * 表单输入数据
		 **********************************************************************/
		var dataJson = userSettings.dataJson;

		var tt_name = userSettings.tt_name;

		/***********************************************************************
		 * 自定义组件
		 ************************************************************************/
		this.ccts	=	new Array();
		
		this.showTT_name = function() {
			return tt_name;
		}
		
		/***********************************************************************
		 * 视图初始化
		 **********************************************************************/
		this.init = function() {

			var cellHtmls = new Object();
			var cells = designConfig.cells;
			if (undefined == cells) {
				return;
			}
			var length = cells.length;
			for ( var x = 0; x < length; x++) {
				var cell = cells[x];
				var row = cell.row;
				var col = cell.col;
				cellHtmls[row + "-" + col] = cell;
			}

			/*******************************************************************
			 * 表单行数
			 ******************************************************************/
			var totalRow = designConfig.totalRow;

			/*******************************************************************
			 * 表单列数
			 ******************************************************************/
			var totalCol = designConfig.totalCol;

			/*******************************************************************
			 * 表单列宽配置
			 ******************************************************************/
			var columnWidths = designConfig.columnWidths;

			/*******************************************************************
			 * 表单行高配置
			 ******************************************************************/
			var rowHeights = designConfig.rowHeights;

			var objectForMerge = new Object();
			var array = new Array();
			for ( var rowIndex = 0; rowIndex < totalRow + 1; rowIndex++) {
				if (0 == rowIndex) {
					instance.generateThead(totalCol, columnWidths, array);
				} else {
					array.push("<tr>");
					for ( var cellIndex = 1; cellIndex < totalCol + 1; cellIndex++) {
						if (rowIndex == 0 && cellIndex == 0) {
							array
									.push("<th class='cellHeader'  style='text-align:center;width:40px;height:26px;' align='center' >&nbsp;</th>");
						} else if (rowIndex == 0) {
							array
									.push("<th style='text-align:center;width:"
											+ columnWidths[cellIndex - 1]
											+ "px;' align='center' "
											+ generateTH(rowIndex, cellIndex)
											+ "><div class='dragheader' onmousedown='MouseDownToResize(event,this);'>"
											+ cellIndex + "</div></th>");
						} else if (cellIndex == 0) {
							array.push("<th style='text-align:center;height:"
									+ rowHeights[rowIndex - 1] + "px;' "
									+ generateTH(rowIndex, cellIndex) + ">"
									+ rowIndex + "</th>");
						} else {
							instance.generateTD(rowIndex, cellIndex, array,
									cellHtmls, objectForMerge);
						}
					}
					array.push("</tr>");
				}

			}
			rootElement.html(array.join(""));

			if (undefined == inputConfig) {
				return;
			}
			for (key in inputConfig) {
				var input = inputConfig[key];
				var strs = key.split("-");
				var row = strs[0];
				var col = strs[1];
				var td = instance.getTD(row, col);
				if ("text" == input.inputType) {
					generateTextInput(td, input, dataJson);
				} else if ("textArea" == input.inputType) {
					generateTextAreaInput(td, input, dataJson);
				} else if ("fatText" == input.inputType) {
					generateFatTextAreaInput(td, input, dataJson);
				} else if ("select" == input.inputType) {
					generateSelectInput(td, input, dataJson);
				} else if ("radio" == input.inputType) {
					generateRadioInput(td, input, dataJson);
				} else if ("check" == input.inputType) {
					generateCheckInput(td, input, dataJson);
				} else if ("date" == input.inputType) {
					generateDateInput(td, input, dataJson);
				} else if ("time" == input.inputType) {
					generateTimeInput(td, input, dataJson);
				} else if ("autoComplete" == input.inputType) {
					generateAutoCompleteInput(td, input, dataJson);
				} else if ("comTree" == input.inputType) {
					generateComTreeInput(td, input, dataJson);
				} else if ("label" == input.inputType) {
					generateLabelInput(td, input, dataJson);
				} else if ("fj" == input.inputType) {
					generateFJInput(td, input, dataJson);
				}else{
					var cct	=	generateCostomCompent(inputConfig,td, input, dataJson);
					instance.ccts[instance.ccts.length]	=	cct;
				}

			}
		};

		/***********************************************************************
		 * 生成单元格
		 * 
		 * @param rowIndex
		 *            行号
		 * @param cellIndex
		 *            列号
		 * @param array
		 *            数据集合
		 * @returns
		 **********************************************************************/
		this.generateTD1 = function(rowIndex, cellIndex, array, cellHtmls) {
			var cell = cellHtmls[rowIndex + "-" + cellIndex];
			var html = "&nbsp;";
			if (undefined != cell && undefined != cell.html) {
				html = cell.html;
			}
			array.push("<td>" + html + "</td>");
		}

		this.generateTD = function(rowIndex, cellIndex, array, cellHtmls,
				objectForMerge) {
			var cell = cellHtmls[rowIndex + "-" + cellIndex];
			var isHide = false;
			if (undefined != objectForMerge[rowIndex + "-" + cellIndex]) {
				isHide = true;
			}

			if (typeof (cell) != "undefined") {
				var content = "<td ";
				if (typeof (cell.style) != "undefined" && "" != cell.style) {
					var style = cell.style;
					if (true == isHide) {
						style += " display:none;";
					}
					content += " style=\"" + style + "\"";
				} else {
					if (true == isHide) {
						var style = " display:none;";
						content += " style=\"" + style + "\"";
					}
				}

				if (typeof (cell.cla) != "undefined" && "" != cell.cla) {
					content += " class=\"" + cell.cla + "\"";
				}

				for ( var key in cell) {
					if ("html" != key && "style" != key && "cla" != key) {
						content += " " + key + "=\"" + cell[key] + "\"";
					}
				}

				if (parseInt(cell.rowspan) > 1 || parseInt(cell.colspan) > 1) {
					var rowspan = parseInt(cell.rowspan);
					var colspan = parseInt(cell.colspan);
					// console.info(cell.row+"-"+cell.col+" 单元格"+cell.rowspan+"
					// = "+cell.colspan+"="+cell.row+"-"+cell.col);
					if (rowspan > 1) {

					} else {
						rowspan = 1;
					}

					if (colspan > 1) {

					} else {
						colspan = 1;
					}
					for ( var x = rowIndex; x <= rowIndex + rowspan - 1; x++) {
						for ( var y = cellIndex; y <= cellIndex + colspan - 1; y++) {
							if (x == rowIndex && y == cellIndex) {

							} else {
								var obj = new Object();
								obj.row = rowIndex;
								obj.col = cellIndex;
								objectForMerge[x + "-" + y] = obj;

								// console.info(x+"-"+y+"===="+instance.objectForMerge[x
								// + "-" + y]);
								var hi = new Object();
								hi.row = x;
								hi.col = y;
								hi.rowFrom = rowIndex;
								hi.colFrom = cellIndex;
								// instance.hideTDs[instance.totalForHide] = hi;
								// instance.totalForHide++;
							}
						}
					}
				}

				if (typeof (cell.html) != "undefined" && "&nbsp;" != cell.html
						&& "" != cell.html) {
					content += ">" + cell.html + "</td>";
				} else {
					content += "></td>";
				}
				array.push(content);
			} else {
				if (true == isHide) {
					array.push("<td style='display:none;'>&nbsp;</td>");
				} else {
					array.push("<td>&nbsp;</td>");
				}

			}
		}

		/***********************************************************************
		 * 生成thread
		 **********************************************************************/
		this.generateThead = function(totalCol, columnWidths, array) {
			array.push("<tr class='firstTR'>");
			for ( var x = 0; x < totalCol; x++) {
				array.push("<th width=" + columnWidths[x] + "px></th>");
			}
			array.push("</tr>");
		}

		/***********************************************************************
		 * 获取单元格坐标
		 **********************************************************************/
		this.getTdLocation = function(td) {
			var location = {
				row : 0,
				col : 0,
				rowspan : 0,
				colspan : 0
			};
			location.col = parseInt(td.cellIndex);
			if (null != td.parentNode) {
				location.row = parseInt(td.parentNode.rowIndex);
			} else {
				return;
			}
			location.colspan = parseInt(td.colSpan);
			location.rowspan = parseInt(td.rowSpan);
			return location;
		}

		/***********************************************************************
		 * 表单单元格生成
		 **********************************************************************/
		this.getTD = function(rowIndex, colIndex) {
			var table = rootElement[0], tbody, row, td;
			if (!table || !(tbody = table.children[0])
					|| !(row = tbody.children[rowIndex])
					|| !(td = row.children[colIndex - 1])) {
				return $('<td />');
			}
			return $(td);
		}

		this.inputChecks = function() {

			if (undefined == inputConfig) {
				return true;
			}

			var infos = new Object();
			for ( var key in inputConfig) {
				var config = inputConfig[key];

				if (undefined != config && undefined != config.field) {
					var inputCompent = $("#infos_" + config.field);
					if ("text" == config.inputType
							|| "textArea" == config.inputType
							|| "autoComplete" == config.inputType) {
						var value = checkTextField(config, inputCompent);
						if ("boolean" == typeof (value) && false == value) {
							return false;
						}
						infos[config.field] = value;
					}
					if ("fatText" == config.inputType) {
						var oEditor = CKEDITOR.instances["infos_"
								+ config.field];
						var data = oEditor.getData();
						infos[config.field] = data;
					} else if ("check" == config.inputType) {
						// 复选框
						var value = checkCheckField(config, inputCompent);
						if ("boolean" == typeof (value) && false == value) {
							return false;
						}
						infos[config.field] = value;
					} else if ("radio" == config.inputType) {
						// 单选框
					} else if ("select" == config.inputType) {
						// 下拉框
						var value = checkSelectField(config, inputCompent);
						if ("boolean" == typeof (value) && false == value) {
							return false;
						}
						infos[config.field] = value;
					} else if ("date" == config.inputType) {
						// 日期输入
						var value = checkDateField(config, inputCompent);
						if ("boolean" == typeof (value) && false == value) {
							return false;
						}
						infos[config.field] = value;
					} else if ("time" == config.inputType) {
						// 时间输入
						var value = checkTimeField(config, inputCompent);
						if ("boolean" == typeof (value) && false == value) {
							return false;
						}
						infos[config.field] = value;
					} else if ("autoComplete" == config.inputType) {
						// 联想输入
					} else if ("comTree" == config.inputType) {
						// 下拉列表树
						var value = checkComTreeField(config, inputCompent);
						if ("boolean" == typeof (value) && false == value) {
							return false;
						}
						infos[config.field] = value;
					} else if ("fj" == config.inputType) {
						var value = checkFJField(config, inputCompent);
						infos[config.field] = value;
					}else{
						
					}
				}
			}
			
			for(var x=0; x<instance.ccts.length;x++){
				var cct	=	instance.ccts[x];
				var value	=	cct.check();
				if(undefined != value){
					for(var key in value){
						infos[key]	=	value[key];
					}
				}
			}
			return infos;
		}
	};

	$.fn.formInput = function(action) {
		var i, ilen, args, output, userSettings, $this = this.first(), instance = $this
				.data('formInput');
		if (typeof action !== 'string') {
			userSettings = action || {};
			if (instance) {
				instance.updateSettings(userSettings);
			} else {
				instance = new FormInput($this, userSettings);
				$this.data('formInput', instance);
				instance.init();
			}
			return $this;
		} else {
			args = [];
			if (arguments.length > 1) {
				for (i = 1, ilen = arguments.length; i < ilen; i++) {
					args.push(arguments[i]);
				}
			}

			if (instance) {
				if (typeof instance[action] !== 'undefined') {
					output = instance[action].apply(instance, args);
				} else {
					throw new Error('ListView do not provide action: ' + action);
				}
			}
			return output;
		}
	}
})(jQuery);